<?php


namespace app\admin\controller;


use think\admin\Controller;
use think\Db;

/**
 * 设备管理
 * Class Equip
 * @package app\admin\controller
 */
class Equip extends Controller
{
    /**
     * 绑定数据表
     * @var string
     */
    private $table = 'lgEquip';

    private $status = [
        1 => '在线',
        2 => '离线',
        3 => '异常',
    ];

    private $is_come = [
        1 => '进',
        2 => '出'
    ];

    private $channel_state = [
        0 => '通道离线',
        1 => '通道上线'
    ];

    protected $rule = [
        'name' => 'require',
        'equip_id' => 'require',
        'channel_id' => 'require',
        'channel_name' => 'require',
        'is_come' => 'require',
        'position' => 'require',
        'skill' => 'require',
        'shop_id' => 'require',
        'ip' => 'require',
    ];

    protected $msg = [
        'name.require' => '请输入设备名称',
        'equip_id.require' => '请输入设备ID',
        'channel_name.require' => '请输入通道名称',
        'channel_id.require' => '请输入通道ID',
        'is_come.require' => '请选择是否进入',
        'ip.require' => '请输入ip地址',
        'skill.require' => '请输入技能',
        'position.require' => '请输入位置',
        'shop_id.require' => '请选择门店',
    ];

    /**
     * 设备列表
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        //标题
        $this->title = '设备管理';
        //门店
        $distributor = $this->app->db->table('lg_distributor')->order('id asc')->column('name', 'id');
        $this->assign('distributor', $distributor);
        //搜索
        $equip = input('equip');
        $channel = input('channel');
        $status = input('status');
        $create_at = input('create_at');
        $where = [];
        if ($equip) {
            $where[] = ['e.name|e.equip_id', 'like', '%' . $equip . '%'];
        }
        if ($channel) {
            $where[] = ['e.channel_name|e.channel_id', 'like', '%' . $channel . '%'];
        }
        if ($status) {
            $where[] = ['e.status', '=', $status];
        }
        if ($create_at) {
            $create_at = explode(' - ', $create_at);
            $where[] = ['e.create_time', 'between time', $create_at];
        }
        //链接数据库
        $query = $this->_query($this->table);
        $query->alias('e')->field('e.*,d.name as shop_name');
        $query->join('lg_distributor d', 'e.shop_id  = d.id', 'left');
        $query->where($where)->equal('shop_id');
        $query->order('id desc')->page();
    }

    protected function _index_page_filter(&$data)
    {
        foreach ($data as $key => $value) {
            $value['status_title'] = $this->status[$value['status']];
            $value['is_come_title'] = $this->is_come[$value['is_come']];
            $data[$key] = $value;
        }
    }

    /**
     * 添加设备
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add()
    {
        $this->_applyFormToken();
        //门店
        $distributor = $this->app->db->table('lg_distributor')->order('id asc')->column('name', 'id');
        $this->assign('distributor', $distributor);
        sysoplog('设备管理', '添加数据成功');
        $this->_form($this->table, 'form');
    }

    /**
     * 编辑设备
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit()
    {
        $this->_applyFormToken();
        //门店
        $distributor = $this->app->db->table('lg_distributor')->order('id asc')->column('name', 'id');
        $this->assign('distributor', $distributor);
        sysoplog('设备管理', '编辑数据成功');
        $this->_form($this->table, 'form');
    }

    protected function _form_filter(&$data)
    {
        if ($this->request->isPost()) {
            $params = $this->request->param();
            $where = [];
            if (isset($params['id'])) {
                $where[] = ['id', '<>', $params['id']];
            }
            $isExist = $this->app->db->name($this->table)
                ->where(['equip_id' => $data['equip_id'], 'channel_id' => $data['channel_id']])
                ->where($where)
                ->find();
            if (!empty($isExist)) {
                $this->error('相同设备，通道ID不能重复');
            }
            if (isset($params['id'])) {
                $data['update_time'] = time();
            } else {
                $data['create_time'] = time();
                $data['update_time'] = time();
            }
        }
    }

    /**
     * 异常列表
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function abnormal()
    {
        //标题
        $this->title = '设备管理->异常信息';
        $this->_applyFormToken();
        $query = $this->_query('lg_device_status_record')->equal('device_id')->equal('channel_id');
        sysoplog('设备管理', '异常信息查看');
        $query->order('id desc')->page();
    }

    public function _abnormal_page_filter(&$data)
    {
        foreach ($data as $key => $value) {
            $value['channel_state_title'] = $this->channel_state[$value['channel_state']] ?? '-';
            $data[$key] = $value;
        }
    }

    /**
     * 删除设备
     * @auth true
     * @throws \think\db\exception\DbException
     */
    public function remove()
    {
        $this->_applyFormToken();
        sysoplog('设备管理', '删除数据成功');
        $this->_delete($this->table);
    }
}